package yuan.deng.controller;

import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import yuan.deng.domain.store.Company;
import yuan.deng.service.impl.store.CompanyServiceImpl;
import yuan.deng.service.store.CompanyService;
import yuan.deng.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/store/company/*")
public class CompanyServlet extends BaseServlet {
    private CompanyService companyService=new CompanyServiceImpl();
        //查询所有
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String page = req.getParameter("page");
        //调用查询所有方法
        List<Company> list = companyService.findAll(page);
        //分页数据
        PageInfo<Company> pageInfo=new PageInfo<>(list);
        //发送数据到前台
        req.setAttribute("page",pageInfo);
        //服务器端跳转
        req.getRequestDispatcher("/pages/company/list.jsp").forward(req,resp);
    }
    //新增公司
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前台表格数据
        Map<String, String[]> map = req.getParameterMap();
        Company c=new Company();
        try {
            //封装
            BeanUtils.populate(c,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //生成id,
        String id = UUID.randomUUID().toString().replace(" ", "-");
        c.setId(id);
        //调用保存方法
        companyService.save(c);

        list(req,resp);
    }
    //新增回显
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Company company = companyService.findById(id);
        //发送回显数据
        req.setAttribute("company",company);
        //页面跳转
        req.getRequestDispatcher("/pages/company/update.jsp").forward(req,resp);
    }
    //新增的数据
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Company company=new Company();
        try {
            BeanUtils.populate(company,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        companyService.update(company);
        list(req,resp);
    }
    //删除方法
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String[] strings = id.split(",");
        companyService.delete(strings);
        list(req,resp);
    }
}
